package com.bw.module_announcement.ui


import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_common.appContext
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseFragment
import com.bw.lib_log.utils.LoggerUtil
import com.bw.module_announcement.R
import com.bw.module_announcement.adapter.AnnouncementAdapter
import com.bw.module_announcement.databinding.FragmentAnnouncementBinding
import com.bw.module_announcement.viewmodel.AnnouncementViewModel
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
@Route(path = Router.Ui.AnnouncementFragment)
class AnnouncementFragment : BaseFragment<AnnouncementViewModel, FragmentAnnouncementBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_announcement

    private val  mViewModel: AnnouncementViewModel by viewModels()
    private  var  announcementAdapter: AnnouncementAdapter =AnnouncementAdapter()

    override fun initData() {
        mViewModel.getAnnouncements(2,10)

    }

    override fun initView() {

        mBinding.noticeRv.layoutManager = LinearLayoutManager(requireContext())
        mBinding.noticeRv.adapter = announcementAdapter

    }

    override fun initListener() {
        mViewModel.announcementLiveData_.observe(this){
           // .filter { it.title=="社区活动"||it.title=="安全提醒"||it.title=="环境维护" }
            announcementAdapter.setList(it.filter { it.title!=""&&it.title!="曾山峰爱导管" })
            announcementAdapter.notifyDataSetChanged()
            LoggerUtil.d("hjj",""+it.size)
        }

        announcementAdapter.addChildClickViewIds(R.id.notice_status)

        announcementAdapter.setOnItemChildClickListener{
            adapter,view,position->
            LoggerUtil.d("hjj",""+position)
            when(view.id){
                R.id.notice_status->{
                    if(announcementAdapter.data[position].status=="已阅读"){
                        announcementAdapter.data[position].status="立即报名"
                        announcementAdapter.notifyItemChanged(position)
                    }else if(announcementAdapter.data[position].status=="未阅读"){
                        announcementAdapter.data[position].status="已阅读"
                        announcementAdapter.notifyItemChanged(position)
                    }else if(announcementAdapter.data[position].status=="立即报名"){
                       ToastUtils.showLong("报名成功")
                    }
                }
            }
        }
    }





}